const mysql = require("mysql2")

// 1.创建连接池
const connectionPool = mysql.createPool({
  host: "localhost",
  port: 3306,
  database: "coderhub_new",
  user: "root",
  password: "admin123",
  //
  connectionLimit: 5,
})

// 2.查看是否连接成功
connectionPool.getConnection((err, connection) => {
  if (err) {
    console.log("连接失败", err)
    return
  }

  connection.connect((err) => {
    if (err) {
      console.log("和数据库交互失败", err)
    } else {
      console.log("数据库连接成功,可以操作数据库")
    }
  })
})

// 3.获取连接对象
const connection = connectionPool.promise()

module.exports = connection
